package com.gitee.sidihuo.utils.base;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class RequestHolder {

    private static final ThreadLocal<String> requestThreadLocal = new ThreadLocal<>();

    public static void setRequestInfo(String requestInfo) {
        requestThreadLocal.set(requestInfo);
    }

    public static String getRequestInfo() {
        return requestThreadLocal.get();
    }

    public static void setRequestObject(Object object) {
        String jsonString = JSONObject.toJSONString(object);
        setRequestInfo(jsonString);
    }

    public static <T> T getRequestObject(Class<T> objectClass) {
        String requestInfo = getRequestInfo();
        if (StringUtils.isBlank(requestInfo)) {
            return null;
        }
        return JSONObject.parseObject(requestInfo, objectClass);
    }

    public static void clear() {
        requestThreadLocal.remove();
    }

}
